package ebedynky.services;

import ebedynky.beans.HibernateUtil;
import ebedynky.beans.BoxOrder;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

/**
 * @author davidon1
 */
public class BoxOrderService {

    /**
     * Seesion - current connection to the db.
     */
    private Session session;

    /**
     * Constructor, retrieves the session (connection).
     */
    public BoxOrderService() {
        this.session = HibernateUtil.getSessionFactory().openSession();
    }

    /**
     * Closes the session (connection).
     * @return success/failure.
     */
    public boolean close() {
        this.session.close();
        return !(this.session.isOpen());
    }

    /**
     * Restarts the session (connection).
     * @return success/failure.
     */
    public boolean restart() {
        this.session.close();
        if (this.session.isOpen()) {
            throw new IllegalStateException("Session could not be closed.");
        }
        this.session = HibernateUtil.getSessionFactory().openSession();
        return this.session.isOpen();
    }

    /**
     * Adds BoxOrder to the db.
     * @param bo BoxOrder to be inserted
     * @return ID of the added BoxOrder
     */
    public int addBoxOrder(BoxOrder bo) {
        session.beginTransaction();
        int id = getMaxBoxOrderID() + 1;
        bo.setBoxOrderID(id);
        id = (Integer) session.save(bo);
        session.getTransaction().commit();
        return (id);
    }

    /**
     * Retrieves the maximum id currently in the database of BoxOrders.
     * @return Max ID
     */
    public int getMaxBoxOrderID() {
        String hql = "select max(b.boxOrderID) from BoxOrder b";
        List list = session.createQuery(hql).list();
        if (list.get(0) != null) {
            Integer maxboid = (Integer) list.get(0);
            return maxboid;
        } else {
            return 0;
        }
    }

    /**
     * Retrieves a BoxOrder identified by the ID.
     * @param id of the BoxOrder
     * @return BoxOrder with the ID given (null if not found)
     */
    public BoxOrder getBoxOrderByID(int id) {
        session.beginTransaction();
        BoxOrder bo = (BoxOrder) session.get(BoxOrder.class, new Integer(id));
        session.getTransaction().commit();
        return (bo);
    }

    /**
     * Retrieves a list of all BoxOrders stored in the db.
     * @return List of all BoxOrders (null if no BoxOrders)
     */
    public List<BoxOrder> getAllBoxOrders() {
        session.beginTransaction();
        List<BoxOrder> boList = session.createQuery("from BoxOrder").list();
        session.getTransaction().commit();
        return (boList);
    }

    /**
     * Updates a specified BoxOrder identified by ID.
     * @param newBoxOrder an updated BoxOrder
     */
    public void updateBoxOrder(BoxOrder newBoxOrder) {
        session.beginTransaction();
        BoxOrder bo = (BoxOrder) session.get(BoxOrder.class, new Integer(newBoxOrder.getBoxOrderID()));
        bo.setBox(newBoxOrder.getBox());
        bo.setBoxesOrdered(newBoxOrder.getBoxesOrdered());
        bo.setCustomer(newBoxOrder.getCustomer());
        bo.setDateOrdered(newBoxOrder.getDateOrdered());
        bo.setOrderitems(newBoxOrder.getOrderitems());
        bo.setRealised(newBoxOrder.isRealised());
        bo.setTotalPrice(newBoxOrder.getTotalPrice());

        session.flush();
        session.getTransaction().commit();
        session.clear();
    }

    /**
     * Removes the BoxOrder from db.
     * @param id BoxOrder ID for removal
     * @return true if successful, false otherwise
     */
    public boolean deleteBoxOrder(int id) {
        try {
            session.beginTransaction();
            BoxOrder bo = (BoxOrder) session.get(BoxOrder.class, new Integer(id));
            session.delete(bo);
            session.getTransaction().commit();
            return (true);
        } catch (HibernateException ex) {
            return (false);
        }
    }
}
